package bank;

import java.util.HashMap;

public class AccountManager {
	private HashMap<String, AccountModel> map;
	private BankClientConnector bc;
	
	public AccountManager(){
		map = new HashMap<String, AccountModel>();
		bc = new BankClientConnector();
		map = bc.getModelUsingNetwork();
	}
	
	public boolean addAccount(AccountModel model){
		AccountModel accountModel = map.get(model.getSsn());
		
		if(accountModel != null)
			return false;
		
		map.put(model.getSsn(), model);
		bc.setModelUsingNetwork(map);
		return true;
	}
	
	public boolean updateAccount(AccountModel model){
		AccountModel accountModel = map.get(model.getSsn());
		
		if(accountModel == null)
			return false;
		
		map.put(model.getSsn(), model);
		bc.setModelUsingNetwork(map);
		return true;
	}
	
	public boolean deleteAccount(String ssn){
		AccountModel accountModel = map.get(ssn);
		
		if(accountModel == null)
			return false;
		
		map.remove(accountModel);
		bc.setModelUsingNetwork(map);
		return true;
	}
	
	public boolean deposit(AccountModel accountModel, Integer amount){
		AccountModel model = map.get(accountModel.getSsn());
		
		if(model == null)
			return false;
		
		String balance = model.getBalance();
		Integer original = Integer.valueOf(balance);
		
		original += amount;
		
		model.setBalance(String.valueOf(original));
		
		updateAccount(model);
		
		return true;
	}
	
	public AccountModel searchAccount(String ssn){
		return map.get(ssn);
	}
	
	public HashMap<String, AccountModel> getList(){
		map = bc.getModelUsingNetwork();
		return map;
	}
	
	public void closeNetwork(){
		bc.close();
	}
	
}
